import { categoryGet } from "@/api/course";
import { CategoryType } from "@/types/course";
import { create } from "zustand";
import { persist } from "zustand/middleware";
interface CateStateType {
  cateList: CategoryType[];
  fetchCategory: () => void;
  addCategory: (arg: CategoryType) => void;
}
export const useCategory = create<CateStateType>()(
  persist(
    (set, get) => {
      return {
        cateList: [],
        fetchCategory: async () => {
          let res = await categoryGet();
          set({ cateList: res.data.results });
        },
        addCategory: (cateObj) => {
          let list = get().cateList; //获取状态机原本的数据
          list.push(cateObj); //向原数据包中新增新数据
          set({ cateList: list });
        },
      };
    },
    {
      name: "category-2304",
    }
  )
);
